package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.dto.ShipmentsDto;
import com.fms.entity.OrdersHistory;
import com.fms.entity.Shipment;
import com.fms.entity.SysUser;
import com.fms.mapper.*;
import com.fms.result.ResultData;
import com.fms.service.ShipmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ShipmentServiceImpl extends ServiceImpl<ShipmentMapper, Shipment> implements ShipmentService {
    private final ShipmentMapper shipmentMapper;

    private final OrdersHardwareMapper ordersHardwareMapper;

    private final SysUserMapper sysUserMapper;

    private final SysDeptMapper sysDeptMapper;

    private final OrdersHistoryMapper ordersHistoryMapper;
    @Override
    public ResultData getShipments(Long id) {
        LambdaQueryWrapper<Shipment> query = new LambdaQueryWrapper<Shipment>();
        query.eq(Shipment::getOrderId, id);
        List<Shipment> shipments = shipmentMapper.selectList(query);
        return ResultData.success(shipments);
    }

    @Override
    public ResultData updateShipments(ShipmentsDto shipments) {
        LambdaQueryWrapper<Shipment> query = new LambdaQueryWrapper<Shipment>();
        query.eq(Shipment::getOrderId,shipments.getOrderId());
        shipmentMapper.delete(query);
        if (shipments.getShipments().size()<=0){
            return ResultData.success("更新成功");
        }
        for (Shipment shipment : shipments.getShipments()){
            shipment.setShipmentTitle(shipment.getProduct());
            shipmentMapper.insert(shipment);
        }
        OrdersHistory ordersHistory = new OrdersHistory();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, shipments.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("订单出货单修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(shipments.getOrderId());
        ordersHistory.setOperate(0);
        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,shipments.getOrderId())
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("更新成功");
    }
}
